package com.ciban.smarthome.module.db.dao;

import java.util.ArrayList;

import com.ciban.smarthome.module.Constants;
import com.ciban.smarthome.module.entity.CommType;
import com.ciban.smarthome.module.entity.EquiTypeInfo;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

/**
 * 对于外部数据库的封装类
 */
public class ConfigDao {
	private Context context;
	private String db_path;
	private SQLiteDatabase db;
	private Cursor cursor;

	public ConfigDao(Context context, String dbName) {
		this.context = context;
		this.db_path = Constants.DATABASE_PATH + dbName;
	}

	public ConfigDao() {
		this.db_path = Constants.DATABASE_PATH + "config";
	}

	public int getDbVersion() {
		int version = -1;
		cursor = query("version", new String[] { "version_no" }, null, null);

		if (cursor != null) {
			if (cursor.moveToNext()) {
				version = cursor.getInt(0);
			}
		}
		close();
		return version;
	}

	public ArrayList<CommType> getConnList() {
		ArrayList<CommType> list = new ArrayList<CommType>();
		cursor = query("conn_type", new String[] { "type", "name" }, null, null);
		if (cursor != null) {
			CommType info;
			while (cursor.moveToNext()) {
				info = new CommType();
				info.setType(cursor.getInt(0));
				info.setName(cursor.getString(1));

				list.add(info);
			}
		}
		close();
		return list;
	}

	public CommType getConn(int type) {
		CommType info = null;
		cursor = query("conn_type", new String[] { "type", "name" }, "type=?",
				new String[] { type + "" });
		if (cursor != null) {
			if (cursor.moveToNext()) {
				info = new CommType();
				info.setType(cursor.getInt(0));
				info.setName(cursor.getString(1));
			}
		}
		close();
		return info;
	}

	public ArrayList<EquiTypeInfo> getEquipList() {
		ArrayList<EquiTypeInfo> list = new ArrayList<EquiTypeInfo>();
		cursor = query("equipments", new String[] { "type", "name", "icon",
				"modify_view", "detail_view" }, null, null);
		if (cursor != null) {
			EquiTypeInfo info;
			while (cursor.moveToNext()) {
				info = new EquiTypeInfo();
				info.setType(cursor.getInt(0));
				info.setName(cursor.getString(1));
				info.setIcon_path(cursor.getString(2));
				info.setModifiy_view(cursor.getString(3));
				info.setDetails_view(cursor.getString(4));

				list.add(info);
			}
		}
		close();
		return list;
	}

	public EquiTypeInfo getEquip(int type) {
		EquiTypeInfo info = null;
		cursor = query("equipments", new String[] { "type", "name", "icon",
				"modify_view", "detail_view" }, "type=?", new String[] { type
				+ "" });
		if (cursor != null) {
			if (cursor.moveToNext()) {
				info = new EquiTypeInfo();
				info.setType(cursor.getInt(0));
				info.setName(cursor.getString(1));
				info.setIcon_path(cursor.getString(2));
				info.setModifiy_view(cursor.getString(3));
				info.setDetails_view(cursor.getString(4));
			}
		}
		close();
		return info;
	}

	private Cursor query(String table, String[] cloumns, String where,
			String[] whereArgs) {
		db = SQLiteDatabase.openDatabase(db_path, null,
				SQLiteDatabase.OPEN_READWRITE);

		return db.query(table, cloumns, where, whereArgs, null, null, null);
	}

	private void close() {
		if (cursor != null)
			cursor.close();
		if (db != null)
			db.close();
	}
}
